package com.cruise.game.controller.game;

import com.cruise.game.common.api.ApiResult;
import com.cruise.game.controller.BaseController;
import com.cruise.game.model.vo.admin.GameCategoryVO;
import com.cruise.game.service.admin.AdminCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cruise.game.model.entity.GameCategory;
import com.cruise.game.mapper.GameCategoryMapper;
import com.cruise.game.mapper.GameServiceMapper;

/**
 * 游戏分类控制器
 * 
 * @author cruise
 * @date 2025-02-28
 */
@Slf4j
@Api(tags = "游戏分类相关接口")
@RestController
@RequestMapping("/api/game/categories")
public class CategoryController extends BaseController {

    @Resource
    private AdminCategoryService adminCategoryService;

    @Resource
    private GameCategoryMapper gameCategoryMapper;

    @Resource
    private GameServiceMapper gameServiceMapper;

    /**
     * 获取游戏分类列表
     */
    @ApiOperation("获取游戏分类列表")
    @GetMapping
    public ApiResult<List<GameCategoryVO>> getCategoryList() {
        // 直接使用管理员的分类服务获取活跃状态的分类列表，不使用分页
        LambdaQueryWrapper<GameCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GameCategory::getStatus, "active");
        queryWrapper.orderByDesc(GameCategory::getCreatedAt);

        List<GameCategory> categories = gameCategoryMapper.selectList(queryWrapper);

        List<GameCategoryVO> categoryVOs = categories.stream()
                .map(category -> {
                    GameCategoryVO vo = new GameCategoryVO();
                    BeanUtils.copyProperties(category, vo);
                    // 查询服务数量
                    Integer serviceCount = gameServiceMapper.countByCategoryId(category.getId());
                    vo.setServiceCount(serviceCount);
                    return vo;
                })
                .collect(Collectors.toList());

        return ApiResult.success(categoryVOs);
    }

    /**
     * 获取游戏分类详情
     */
    @ApiOperation("获取游戏分类详情")
    @GetMapping("/{id}")
    public ApiResult<GameCategoryVO> getCategoryDetail(
            @ApiParam(value = "游戏分类ID", required = true) @PathVariable("id") Long id) {
        return ApiResult.success(adminCategoryService.getCategoryDetail(id));
    }
}
